package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishesMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetMealMapper setMealMapper;
    @Autowired
    SetMealDishesMapper setMealDishesMapper;

    @Autowired
    DishMapper dishMapper;
    @Override
    public void saveSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.insert(setmeal);
        List<SetmealDish> setMealDishes = setmealDTO.getSetmealDishes();
        if (setMealDishes!=null&&!setMealDishes.isEmpty()){
            setMealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setMealDishesMapper.insert(setMealDishes);

    }

    @Override
    public PageResult getPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult result = new PageResult();
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.getPage(setmealPageQueryDTO);
        result.setRecords(page.getResult());
        result.setTotal(page.getTotal());
        return result;

    }

    @Override
    public void batchDeleteMeal(String ids) {
        String[] split = ids.split(",");
        //  JDK8 Stream API
        Long[] batchId = Arrays.stream(split)
                .map(Long::parseLong)
                .toArray(Long[]::new);

        setMealMapper.batchDeleteMeal(batchId);
        setMealDishesMapper.batchDeleteBySetmealId(batchId);
    }

    @Override
    public SetmealVO getSetMealById(Long id) {
        SetmealVO setmealVO = setMealMapper.getSetMealById(id);
        List<SetmealDish> setmealDishes= setMealDishesMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;

    }

    @Override
    public void updateSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setMealMapper.updateSetMeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish ->{
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setMealDishesMapper.batchDeleteBySetmealId(new Long[]{setmeal.getId()});
        setMealDishesMapper.insert(setmealDishes);

    }

    @Override
    public void startOrStop(Long id, Integer status) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.updateSetMeal(setmeal);
    }


    @Override
    public List<Setmeal> getList(Long categoryId) {

        return setMealMapper.getList(categoryId);

    }

    @Override
    public List<DishItemVO> getSetmealDishItem(Long id) {

        List<SetmealDish> setmealDishes = setMealDishesMapper.selectBySetmealId(id);
        List<DishItemVO> dishItemVOS = new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishes) {
            DishItemVO dishItemVO = new DishItemVO();
            Dish dish = dishMapper.getDishImageAndDescription(setmealDish.getDishId());
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setName(setmealDish.getName());
            dishItemVO.setCopies(setmealDish.getCopies());
            dishItemVO.setDescription(dish.getDescription());
            dishItemVOS.add(dishItemVO);
        }
        return dishItemVOS;
    }
}
